package com.hsgene.authority.controller;

import com.hsgene.authority.dto.OperationPermissionTree;
import com.hsgene.authority.exception.AuthorityErrorCode;
import com.hsgene.authority.service.OperationAccountPermissionService;
import com.hsgene.authority.service.PermissionService;
import com.hsgene.common.util.resolver.FromSession;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.web.session.UserInfo;
import com.hsgene.common.util.web.session.UserInfoExtra;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 权限管理Controller
 *
 * @author wangbing
 * @version 1.0, 2018/11/1
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/user/permission")
public class UserPermissionController {

    @Resource
    private PermissionService permissionService;

    @Resource
    private OperationAccountPermissionService operationAccountPermissionService;


    /**
     * 获取登录用户的详细权限信息
     *
     * @param userInfo 登录用户信息
     * @return 详细权限信息
     */
    @GetMapping("")
    public ApiResult<OperationPermissionTree> getUserDepartmentPermission(
            @FromSession UserInfo userInfo) {
        UserInfoExtra extra = userInfo.getExtra();
        if (extra == null) {
            return ApiResult.succ();
        }
        if (Boolean.TRUE.equals(extra.getSystemSuper())) {
            return operationAccountPermissionService.getSystemSuperPermission();
        }
        if (StringUtils.isEmpty(extra.getDepartmentId())) {
            return ApiResult.succ();
        }
        return operationAccountPermissionService.findDepartmentPermission(extra.getDepartmentId(), true, true);
    }

    @GetMapping("{permission_id}")
    public ApiResult<OperationPermissionTree> findPermission(
            @PathVariable("permission_id") String permissionId,
            @RequestParam(name = "containParent" , defaultValue = "false") Boolean containParent,
            @RequestParam(name = "containChildren", defaultValue = "false") Boolean containChildren,
            @FromSession UserInfo userInfo) {
        UserInfoExtra extra = userInfo.getExtra();
        if (extra == null) {
            return ApiResult.succ();
        }
        if (Boolean.TRUE.equals(extra.getSystemSuper())) {
            return operationAccountPermissionService.getSystemSuperPermission();
        }
        if (StringUtils.isEmpty(extra.getDepartmentId())) {
            return ApiResult.succ();
        }
        ApiResult<OperationPermissionTree> result = operationAccountPermissionService.findDepartmentPermission(extra.getDepartmentId(), true, true);
        if (!result.isSucc()) {
            return result;
        }
        OperationPermissionTree tree = permissionService.getPermissionTree(permissionId, false);
        if (tree == null) {
            return ApiResult.fail(AuthorityErrorCode.PERMISSION_NOT_EXISTED);
        }
        if (containParent) {
            permissionId = tree.getParentId();
        }
        return ApiResult.succ(findPermission(result.getData(), permissionId, containChildren));
    }

    private OperationPermissionTree findPermission(OperationPermissionTree tree, String permissionId, Boolean containChildren) {
        if (permissionId.equals(tree.getId())) {
            if (!containChildren) {
                tree.setChildrenPermissions(null);
            }
            return tree;
        }
        if (!CollectionUtils.isEmpty(tree.getChildrenPermissions())) {
            for (OperationPermissionTree permissionTree : tree.getChildrenPermissions()) {
                OperationPermissionTree result = findPermission(permissionTree, permissionId, containChildren);
                if (result != null) {
                    return result;
                }
            }
        }
        return null;
    }
}
